package leetcode.code;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution125 implements IDebug {

	public boolean isPalindrome(String s) {
		if (s.length() == 1)
			return true;
		int l = 0;
		int r = s.length() - 1;
		char[] c = s.toCharArray();
		while (l <= r) {
			while (l <= r && !this.validate(c[l])) {
				l++;
			}
			while (l <= r && !this.validate(c[r])) {
				r--;
			}
			if (l <= r && this.toLowerCase(c[l++]) != this.toLowerCase(c[r--])) {
				return false;
			}
		}
		return true;
	}

	private char toLowerCase(char c) {
		if ((c >= 'A' && c <= 'Z')) {
			return (char) (c + 32);
		}
		return c;
	}

	private boolean validate(char c) {
		if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
			return true;
		}
		return false;
	}

	@Override
	public void debug1() {
		String s = "A man, a plan, a canal: Panama";
		boolean e = true;
		boolean r = this.isPalindrome(s);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
	}

	@Override
	public void debug3() {
		String s = ".,";
		boolean e = true;
		boolean r = this.isPalindrome(s);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution125 so = new Solution125();
		so.debug1();
		so.debug3();
		System.out.println('a' - 'A');
	}

}
